<?php


namespace BoojasrFramework;


use BoojasrFramework\ServicesContainer\ContainerBuilder;

class PackageManage
{
    use ContainerAwareTrait;

    public function build(ContainerBuilder $container){
        $runInstances = $this->getRunInstances();
        foreach ($runInstances as $runInstance){
            if ($runInstance instanceof RunInterface){
                $runInstance->build($container);
            }
        }
    }

    public function boot(){
        $runInstances = $this->getRunInstances();
        foreach ($runInstances as $runInstance){
            if ($runInstance instanceof RunInterface){
                $runInstance->setContainer($this->container);
                $runInstance->boot();
            }
        }
    }

    /**
     * @return array|mixed
     * @throws \ReflectionException
     */
    public function getRunInstances(){
        static $runInstancesList;
        if (!$runInstancesList){
            $package = dirname(__DIR__)."/config/package.php";
            $tmpList = [];
            if (file_exists($package)){
                $runList = require_once $package;
                $runList = array_merge($runList,[KernelRun::class]);
                foreach ($runList as $run){
                    $runReflection = new \ReflectionClass($run);
                    $runInstance = $runReflection->newInstance();
                    $tmpList[] = $runInstance;
                }
            }
            $runInstancesList = $tmpList;
        }
        return $runInstancesList;
    }
}